﻿using System;
using System.Windows.Forms;
using TestCenter.Classes;

namespace TestCenter.Forms
{
    public partial class FormLogin : Form
    {
        public FormLogin()
        {
            InitializeComponent();
        }

        private void BtnExitClick(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void BtnLoginClick(object sender, EventArgs e)
        {
            AttemptLogin();
        }

        private void AttemptLogin()
        {
            var username = tbUsername.Text;
            var password = tbPassword.Text;

            if (Global.CurrentUser == null)
                Global.CurrentUser = new User();

            if (Connection.IsValidAccount(username, password, ref Global.CurrentUser))
            {
                lbLoginStatus.Text = "Successed";
                DoLogin();
            }
            else
            {
                lbLoginStatus.Text = "Failed";
            }
        }

        private void DoLogin()
        {
            Visible = false;
            if (Global.FrmUser == null || Global.FrmUser.IsDisposed)
                Global.FrmUser = new FormUser();
            Global.FrmUser.Show();
            Global.FrmUser.BringToFront();
        }
    }
}
